﻿using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using rInvoice.Utils;

namespace rInvoice
{
    public partial class CompanyAccountInputPanel : Form
    {
        private ServerObject mServerObject = null;
        bool mIsLoading = false;

        int mAccountID = 0;
        int mCompanyID = 0;
        string mAccountName = string.Empty;
        int mValutaID = 0;
        int mBankID = 0;
        string mAccountNumber = string.Empty;
        string mAccountTrezor = string.Empty;
        int mBankCorespodentID = 0;
        string mAccountCorespodent = string.Empty;
        int mTopID = 0;

        public int AccountID
        {
            get { return mAccountID; }
            set { mAccountID = value; }
        }

        public int CompanyID
        {
            get { return mCompanyID; }
            set { mCompanyID = value; }
        }

        public string AccountName
        {
            get { return mAccountName; }
            set { mAccountName = value; }
        }

        public int ValutaID
        {
            get { return mValutaID; }
            set { mValutaID = value; }
        }

        public int BankID
        {
            get { return mBankID; }
            set { mBankID = value; }
        }

        public string AccountNumber
        {
            get { return mAccountNumber; }
            set { mAccountNumber = value; }
        }

        public string AccountTrezor
        {
            get { return mAccountTrezor; }
            set { mAccountTrezor = value; }
        }

        public int BankCorespodentID
        {
            get { return mBankCorespodentID; }
            set { mBankCorespodentID = value; }
        }

        public string AccountCorespodent
        {
            get { return mAccountCorespodent; }
            set { mAccountCorespodent = value; }
        }

        public int TopID
        {
            get { return mTopID; }
            set { mTopID = value; }
        }



        public CompanyAccountInputPanel()
        {
            InitializeComponent();
            mServerObject = new ServerObject();
        }

        private void FillCompanyCombo()
        {
            DataTable clTable = mServerObject.SelectCompanyList(false);
            companyListComboBox.DataSource = clTable;
            companyListComboBox.DisplayMember = "CompanyName";
            companyListComboBox.ValueMember = "CompanyID";
        }

        private void FillBankCombo()
        {
            DataTable bTable = mServerObject.SelectBankList(false);
            bankComboBox.DataSource = bTable;
            bankComboBox.DisplayMember = "BankID";
            bankComboBox.ValueMember = "BankName";
        }

        private void FillValutaCombo()
        {
            DataTable vTable = mServerObject.GetClassifiersListByTypeID(UFT.Table_TypeID.NAME_Valuta);
            valutaComboBox.DataSource = vTable;
            valutaComboBox.DisplayMember = "Name";
            valutaComboBox.ValueMember = "CodeID";
        }

        private void FillBankCorespondentCombo()
        {
            DataTable bcTable = mServerObject.SelectBankList(false);
            bankCorespodentComboBox.DataSource = bcTable;
            bankCorespodentComboBox.DisplayMember = "BankID";
            bankCorespodentComboBox.ValueMember = "BankName";
        }

        private void FillTopIDCombo()
        {
            DataTable topTable = new DataTable();
            topTable.Columns.Add("TopID", typeof(string));
            topTable.Rows.Add("0");

            topIDComboBox.DataSource = topTable;
            topIDComboBox.DisplayMember = "TopID";
            topIDComboBox.ValueMember = "TopID";
        }

        private void CompanyAccountInputPanel_Load(object sender, EventArgs e)
        {
            mIsLoading = true;

            FillCompanyCombo();
            FillBankCombo();
            FillBankCorespondentCombo();
            FillTopIDCombo();
            FillValutaCombo();

            companyListComboBox.SelectedValue = mCompanyID;
            accountNameTextBox.Text = mAccountName;
            valutaComboBox.SelectedValue = mValutaID;
            bankComboBox.SelectedValue = mBankID;
            accountNumberTextBox.Text = mAccountNumber;
            accountTrezorTextBox.Text = mAccountTrezor;
            bankCorespodentComboBox.SelectedValue = mBankCorespodentID;
            accountCorespodentTextBox.Text = mAccountCorespodent;
            topIDComboBox.SelectedValue = mTopID;

            mIsLoading = false;
        }

        private void companyListComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!mIsLoading && companyListComboBox.SelectedIndex != -1)
            {
                int.TryParse(companyListComboBox.SelectedValue.ToString(), out mCompanyID);
            }
        }

        private void valutaComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!mIsLoading && valutaComboBox.SelectedIndex != -1)
            {
                int.TryParse(valutaComboBox.SelectedValue.ToString(), out mValutaID);
            }
        }

        private void bankComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!mIsLoading && bankComboBox.SelectedIndex != -1)
            {
                int.TryParse(bankComboBox.SelectedValue.ToString(), out mBankID);
            }
        }

        private void bankCorespodentComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!mIsLoading && bankCorespodentComboBox.SelectedIndex != -1)
            {
                int.TryParse(bankCorespodentComboBox.SelectedValue.ToString(), out mBankCorespodentID);
            }
        }

        private void topIDComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!mIsLoading && topIDComboBox.SelectedIndex != -1)
            {
                int.TryParse(topIDComboBox.SelectedValue.ToString(), out mTopID);
            }
        }

        private void accountNameTextBox_TextChanged(object sender, EventArgs e)
        {
            mAccountName = accountNameTextBox.Text;
        }

        private void accountNumberTextBox_TextChanged(object sender, EventArgs e)
        {
            mAccountNumber = accountNumberTextBox.Text;
        }

        private void accountTrezorTextBox_TextChanged(object sender, EventArgs e)
        {
            mAccountTrezor = accountTrezorTextBox.Text;
        }

        private void accountCorespodentTextBox_TextChanged(object sender, EventArgs e)
        {
            mAccountCorespodent = accountCorespodentTextBox.Text;
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            if (mServerObject.UpdateCompanyAccount(mAccountID
                                                    ,mCompanyID
                                                    ,mAccountName
                                                    ,mValutaID
                                                    ,mBankID
                                                    ,mAccountNumber
                                                    ,mAccountTrezor
                                                    ,mBankCorespodentID
                                                    ,mAccountCorespodent
                                                    ,mTopID
                                                    ,false))
            {

                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            else
            {
                this.DialogResult = System.Windows.Forms.DialogResult.Abort;
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }
    }
}
